import { defineStore } from "pinia"
import { ref, computed } from 'vue'

export const useCartStore = defineStore('cart', () => {
    let products = ref([])
    let address = ref({})
    let orderIdList = ref([])

    const totalPrice = computed(() => products.value.reduce((pre, cur) => pre + (cur.price * cur.number), 0).toFixed(2))

    const clearAll = () => {
        products.value = []
        address.value = {}
        orderIdList = []
    }

    return {
        products,
        address,
        orderIdList,
        totalPrice,
        clearAll
    }
}, {
    persist: true
})